package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.Set;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllFeatures", query = "select myFeature from Feature myFeature"),
		@NamedQuery(name = "findFeatureByName", query = "select myFeature from Feature myFeature where myFeature.name = ?1"),
		@NamedQuery(name = "findFeatureByNameContaining", query = "select myFeature from Feature myFeature where myFeature.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findFeatureByDescription", query = "select myFeature from Feature myFeature where myFeature.description = ?1"),
		@NamedQuery(name = "findFeatureByDescriptionContaining", query = "select myFeature from Feature myFeature where myFeature.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllFeaturesByDetectmethod", query = "select myFeature from Feature myFeature where myFeature.detectmethod.id = ?1"),
		@NamedQuery(name = "findFeatureById", query = "select myFeature from Feature myFeature where myFeature.id = ?1"),
		@NamedQuery(name = "findFeatureByPrimaryKey", query = "select myFeature from Feature myFeature where myFeature.id = ?1 "),
		@NamedQuery(name = "deleteFeaturesByIds", query = "delete Feature where id in (?1)"),
		@NamedQuery(name = "findFeaturesByIds", query = "select myFeature from Feature myFeature where myFeature.id in (?1)"),
		@NamedQuery(name = "findFeatureByPhyscialPrimaryKey", query = "select myFeature from Feature myFeature where  myFeature.name = ?1 and myFeature.detectmethod.id = ?2"),
		@NamedQuery(name = "countFeaturesByName", query = "select count(myFeature)  from Feature myFeature where myFeature.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "feature")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Feature")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Feature implements Serializable, Comparable<Feature> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "detectmethod", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Detectmethod detectmethod;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "feature", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectdetail> detectdetailsForFeature;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "feature", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectfeature> detectfeaturesForFeature;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "feature", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectstandard> detectstandardsForFeature;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "feature", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Featurestandard> featurestandardsForFeature;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDetectmethod(Detectmethod detectmethod) {
		this.detectmethod = detectmethod;
	}

	public Detectmethod getDetectmethod() {
		return detectmethod;
	}

	public void setDetectdetailsForFeature(Set<Detectdetail> detectdetailsForFeature) {
		this.detectdetailsForFeature = detectdetailsForFeature;
	}

	public Set<Detectdetail> getDetectdetailsForFeature() {
		if (detectdetailsForFeature == null) {
			detectdetailsForFeature = new java.util.LinkedHashSet<Detectdetail>();
		}
		return detectdetailsForFeature;
	}

	public void setDetectfeaturesForFeature(Set<Detectfeature> detectfeaturesForFeature) {
		this.detectfeaturesForFeature = detectfeaturesForFeature;
	}

	public Set<Detectfeature> getDetectfeaturesForFeature() {
		if (detectfeaturesForFeature == null) {
			detectfeaturesForFeature = new java.util.LinkedHashSet<Detectfeature>();
		}
		return detectfeaturesForFeature;
	}

	public void setDetectstandardsForFeature(Set<Detectstandard> detectstandardsForFeature) {
		this.detectstandardsForFeature = detectstandardsForFeature;
	}

	public Set<Detectstandard> getDetectstandardsForFeature() {
		if (detectstandardsForFeature == null) {
			detectstandardsForFeature = new java.util.LinkedHashSet<Detectstandard>();
		}
		return detectstandardsForFeature;
	}

	public void setFeaturestandardsForFeature(Set<Featurestandard> featurestandardsForFeature) {
		this.featurestandardsForFeature = featurestandardsForFeature;
	}

	public Set<Featurestandard> getFeaturestandardsForFeature() {
		if (featurestandardsForFeature == null) {
			featurestandardsForFeature = new java.util.LinkedHashSet<Featurestandard>();
		}
		return featurestandardsForFeature;
	}

	public void copy(Feature that) {

		setId(that.getId());

		setName(that.getName());
		setDescription(that.getDescription());

		setDetectmethod(that.getDetectmethod());

		setDetectdetailsForFeature(new java.util.LinkedHashSet<Detectdetail>(
				that.getDetectdetailsForFeature()));

		setDetectfeaturesForFeature(new java.util.LinkedHashSet<Detectfeature>(
				that.getDetectfeaturesForFeature()));

		setDetectstandardsForFeature(new java.util.LinkedHashSet<Detectstandard>(
				that.getDetectstandardsForFeature()));

		setFeaturestandardsForFeature(new java.util.LinkedHashSet<Featurestandard>(
				that.getFeaturestandardsForFeature()));

	}

	public Feature() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Feature))
			return false;
		Feature comparedFeature = (Feature) obj;
		if (!CheckPropertyEqual(this.name, comparedFeature.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.detectmethod.id, comparedFeature.detectmethod.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Feature o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
